﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace SystemDiagramNew
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class FilterTheSelectedCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			this.Doc = cmdData.Application.ActiveUIDocument.Document;
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			ISelectionFilter selectionFilter = new RiserPipeFilter();
			IList<Element> list;
			List<Element> selElements;
			do
			{
				list = selection.PickElementsByRectangle(selectionFilter, "请框选一根立管");
				selElements = new List<Element>();
			}
			while (list.Count <= 0);
			Element element = list[0];
			string systemName = element.GetSystemName();
			List<Element> systemElementSet = this.GetSystemElementSet(this.Doc, systemName);
			selElements = this.GetAllRisePipeAndFit(element, systemElementSet);
			RevitVersionFuncs.SetSelection(selection, selElements);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private List<Element> GetSystemElementSet(Autodesk.Revit.DB.Document doc, string strSysName)
		{
			new HYSystemElementFilter(doc);
            return FilterTheSelectedCommand.ElementSetToList(this.GetElemSetByParamValue((BuiltInParameter)(-1140324), strSysName));
		}

		public ElementSet GetElemSetByParamValue(BuiltInParameter biParam, string paramValue)
		{
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(this.Doc);
			ElementSet elementSet = this.Doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = hysystemElementFilter.GetFilterByCondition(null, false, null).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element != null)
				{
					Parameter parameter = element.GetParameter(biParam);
					if (parameter != null && parameter.HasValue && parameter.AsString().Trim() != "")
					{
						string[] array = parameter.AsString().Split(new char[]
						{
							','
						});
						for (int i = 0; i < array.Length; i++)
						{
							if (array[i].Equals(paramValue))
							{
								elementSet.Insert(element);
							}
						}
					}
				}
			}
			return elementSet;
		}

		public List<Element> GetAllRisePipeAndFit(Element risePipe, List<Element> listElement)
		{
			List<Element> list = new List<Element>();
			list.Add(risePipe);
			foreach (Element element in listElement)
			{
				List<Connector> connectors = FilterTheSelectedCommand.GetConnectors(element);
				double num = FilterTheSelectedCommand.GetConnectors(risePipe)[0].Origin.X.Fix(6);
				double num2 = FilterTheSelectedCommand.GetConnectors(risePipe)[0].Origin.Y.Fix(6);
				if (connectors.Count == 2)
				{
					if (connectors[0].Origin.X.Fix(6) == num && connectors[0].Origin.Y.Fix(6) == num2 && connectors[1].Origin.X.Fix(6) == num && connectors[1].Origin.Y.Fix(6) == num2)
					{
						list.Add(element);
					}
				}
				else if ((element.Location as LocationPoint).Point.X.Fix(6) == num && (element.Location as LocationPoint).Point.Y.Fix(6) == num2)
				{
					list.Add(element);
				}
			}
			return list;
		}

		public void GetAllRisePipeAndFit(ref List<Element> listRiserPipe, Element ele)
		{
			List<Connector> connectors = FilterTheSelectedCommand.GetConnectors(ele);
			if (connectors.Count == 2)
			{
				if (connectors[0].IsConnected)
				{
					Element owner = this.GetConnectConnector(connectors[0]).Owner;
					List<Connector> connectors2 = FilterTheSelectedCommand.GetConnectors(owner);
					if (connectors2.Count != 2)
					{
						listRiserPipe.Add(owner);
						return;
					}
					if (connectors[0].Origin.X.Fix(6) == connectors2[0].Origin.X.Fix(6) && connectors[0].Origin.Y.Fix(6) == connectors2[0].Origin.Y.Fix(6) && connectors2[0].Origin.Y.Fix(6) == connectors2[1].Origin.Y.Fix(6) && connectors2[0].Origin.X.Fix(6) == connectors2[1].Origin.X.Fix(6))
					{
						listRiserPipe.Add(owner);
						this.GetAllRisePipeAndFit(ref listRiserPipe, owner);
						return;
					}
				}
			}
			else
			{
				int count = connectors.Count;
			}
		}

		public static List<Connector> GetConnectors(Element element)
		{
			new List<Connector>();
			ConnectorSet cs = new ConnectorSet();
			if (element == null)
			{
				return null;
			}
			FamilyInstance familyInstance = element as FamilyInstance;
			if (familyInstance != null && familyInstance.MEPModel != null)
			{
				cs = familyInstance.MEPModel.ConnectorManager.Connectors;
			}
			MEPSystem mepsystem = element as MEPSystem;
			if (mepsystem != null)
			{
				cs = mepsystem.ConnectorManager.Connectors;
			}
			MEPCurve mepcurve = element as MEPCurve;
			if (mepcurve != null)
			{
				cs = mepcurve.ConnectorManager.Connectors;
			}
			return FilterTheSelectedCommand.ConnectorSetToList(cs);
		}

		public static List<Connector> ConnectorSetToList(ConnectorSet cs)
		{
			List<Connector> list = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = cs.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector item = obj as Connector;
				list.Add(item);
			}
			return list;
		}

		public static List<Element> ElementSetToList(ElementSet es)
		{
			List<Element> list = new List<Element>();
			ElementSetIterator elementSetIterator = es.ForwardIterator();
			while (elementSetIterator.MoveNext())
			{
				object obj = elementSetIterator.Current;
				Element item = obj as Element;
				list.Add(item);
			}
			return list;
		}

		public Connector GetConnectConnector(Connector connector)
		{
			Connector result = null;
			if (connector != null && connector.IsConnected)
			{
				ConnectorSetIterator connectorSetIterator = connector.AllRefs.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector2 = obj as Connector;
					if (connector2 != null && connector2.Owner.UniqueId != connector.Owner.UniqueId && ((int)connector2.ConnectorType == 1 || (int)connector2.ConnectorType == 2 || (int)connector2.ConnectorType == 19))
					{
						result = connector2;
						break;
					}
				}
			}
			return result;
		}

		private Document Doc;
	}
}
